package tosaalgortihm;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
//import java.util.LinkedList;
//import java.util.List;

        
public class TosaAlgortihm {

    public static int CHANNLES;   //number of channels
    public static int ITEMS;      // Nuber of items

    public static void AllocationFunction(){
    
    
    }
    
    
    
    
    public static void main(String[] args) throws IOException {

        int i,j;                                                                                //counters
        
        BufferedReader br = new BufferedReader(new FileReader("data.txt"));                     //reading data file

        ArrayList<Double[]> items = new ArrayList<Double[]>();                                  //creating list items
        
        String line;
        
        while ((line = br.readLine()) != null) {                                                //read the input file
            
            System.out.println(line);

            Double[] da = new Double [line.split(" ").length];
            
            i=0;
            
            for(String elem : line.split(" ")){
                
                    da[i] = new Double(elem);

                    i++;
            }
            
            items.add(da);
        }
        
       Double[] Allocation1=new Double[items.get(0).length];                                    //creating one more array node for li*pi
       
       items.add(Allocation1);
       
       double temp1,temp2;
        
       for(i=0; i<items.get(0).length; i++){                                                    //sort channels-decreasing order
        
           for( j=1; j<items.get(0).length-i; j++){
            
                
                if(Math.sqrt((items.get(0)[j-1])*(items.get(1)[j-1])) < Math.sqrt((items.get(0)[j])*(items.get(1)[j]))){
                
                    temp1=items.get(0)[j-1];
                    temp2=items.get(1)[j-1];
                    items.get(0)[j-1]=items.get(0)[j];
                    items.get(1)[j-1]=items.get(1)[j];
                    items.get(0)[j]=temp1;
                    items.get(1)[j]=temp2;
                
                }
                
            }
           
        }
        System.out.print("\nSorted:");
        for(i=0; i<items.get(0).length; i++){System.out.print(" " + items.get(0)[i] +" " );}                //print item list
        
        
        for(i=0; i<items.get(2).length; i++){                                                   //sort channels-decreasing order
            
            for(j=1; j<items.get(2).length-i; j++){
                
                if(items.get(2)[j-1] < items.get(2)[j]){
                
                    temp1=items.get(2)[j-1];
                    
                    items.get(2)[j-1]=items.get(2)[j];
                    
                    items.get(2)[j]=temp1;
                
                }
                
            }
        }
        
        for(i=0; i<items.get(2).length; i++){System.out.println("\nCHANNEL "+ (i+1) + " value " +items.get(2)[i]);}   //print channel list
        
        double B=0.0;
        
        double [] T=new double[items.get(2).length];                                            //declaration of list for T
        //---------------------------Initialization step 3------------------------------------// 
        for(i=0; i<items.get(2).length; i++){   
        
            B=B+Math.sqrt(items.get(2)[i]);                                                     //Calculation ΣBi
            T[i]=Math.sqrt(items.get(2)[i])/items.get(2)[items.get(2).length-1];                //Calculation Ti
            System.out.println("To T[i] exei timi" + T[i]);
            
        }
        
        B=B/Math.sqrt(items.get(2)[items.get(2).length-1]);                         //ΣBi/Βκ----Theloume na allaksoume th diairesh
        
        double[] c =new double [items.get(1).length];                                           //array of ci 
        
        double lower,upper;
     
        //---------------------------Initialization step 4------------------------------------//
        for(i=0; i<items.get(1).length; i=(int)Math.round(i+2*B)){
        //---------------------------Initialization step 5_1st loop---------------------------//
            for(j=0, c[i]=0; j<items.get(2).length; j++){
                
                lower=(int)Math.round(i+c[i]);
                upper=(int)Math.round(Math.min(i+c[i]+T[j],items.get(1).length ));
                
                for(int k=(int)lower; k<=upper; k++){
               
                    items.get(3)[k]=(double)j;
                
                }
                
                if(i+c[i]+T[j]>=items.get(1).length){break;}
                
                c[i]=c[i]+T[j];
            }
          //---------------------------Initialization step 11_2nd loop-------------------------//
            for(j=items.get(2).length-1, c[i]=B; j>=0; j--){

                lower=(int)Math.round(i+c[i]);
                upper=(int)Math.round(Math.min(i+c[i]+T[j],items.get(1).length ));
                
                if(lower>= items.get(1).length){lower=items.get(1).length-1;}
                if(upper>=items.get(1).length ){upper=items.get(1).length-1;}

                for(int k=(int)lower; k<=upper; k++){
               
                    items.get(3)[k]=(double)j;
                
                }
               
                if(i+c[i]+T[j]>=items.get(1).length){break;}
            
                c[i]=c[i]+T[j];
            }  
              
       }
       
       for(int k=0; k<items.get(1).length; k++){System.out.println("Item " + k + " Sto Channel "+ items.get(3)[k]);} 
       
       //--------------------------Permutation----------------------------------------------//
       int t=0;
        
        
        Double[] AB = new Double[items.get(2).length];                                        //Pinakas gia upologismo (pi*li)/Bi
        Double[] A = new Double[items.get(2).length];                                         //Pinakas gia pi*li
        items.add(AB);
        items.add(A);
            
        double max;
        double min;
        double h;

        for(i=0; i<items.get(4).length; i++){               //arxikopoihsh pi*li

             items.get(4)[i]=0.0;

        }

        double min2=0.0;
        int dmin = 0;

        while(true){
            

            for(i = 0; i < items.get(4).length; i++){
                                    
                items.get(4)[i]=0.0;
            }

            for(i=0; i<items.get(2).length; i++){
            
                for(j=0; j<items.get(3).length; j++){
            
                    h=(double)i;  
                    
                    if(items.get(3)[j]== h){

                        items.get(4)[i]=items.get(4)[i]+Math.sqrt(items.get(0)[j]*items.get(1)[j]); //ginomeno pi*li

                    }

                }
                
                items.get(5)[i]=items.get(4)[i];                //pi*li=Ai
                items.get(4)[i]=items.get(4)[i]/items.get(2)[i];   //pi*li/Bi
                
            }

            max = items.get(4)[0];
            min = items.get(4)[0];

            int idmax = 0,idmin = 0;
            //---------------------------Permutation step 2----------------------------------//     
            for(i = 0; i < items.get(4).length; i++){

                if(items.get(4)[i]>max){max = items.get(4)[i]; idmax = i;}         //Euresh Channel me megalutero-mikrotero Logo Ai/Bi
                if(items.get(4)[i] < min){min = items.get(4)[i]; idmin = i;}

            }

            System.out.println("TO IDMAX EINAI " + idmax+ "TO ID MIN EINAI " + idmin);

            boolean flag = true;
            
            //---------------------------Permutation step 3----------------------------------//     
            for(i = 0; i < items.get(3).length; i++){
            
                    if(idmax==items.get(3)[i]){
                    
                        if(flag){                                                  //for the first time in order to get the dmin a value
                            
                            min2 = Math.sqrt(items.get(0)[i]*items.get(1)[i]);
                            dmin = i;
                            flag = false;
                            
                        }
                        
                        double rizamin=Math.sqrt(items.get(0)[i]*items.get(1)[i]);
                        
                        System.out.println("To min2 einai  " + min2 + "\nTo riza min einai  " + rizamin+"\n");
                                
                        if(min2 > rizamin){
                            
                            min2 = Math.sqrt(items.get(0)[i]*items.get(1)[i]);
                            dmin = i;
                        }
                        
                        System.out.println("BGAINW THN IF KAI TO DMIN EINAI" + dmin);
                    }
                }
                
                
                System.out.println("/----------------DMIN of the "+ t +" while loop is:" + dmin + "-------------/");
                
                //---------------------------Left part of if----------------------------------//     
                
                double maxSquare,minSquare,sumSquare;
                
                maxSquare = Math.pow(items.get(4)[idmax], 2.0);       
                minSquare = Math.pow(items.get(4)[idmin], 2.0);
                sumSquare = maxSquare+minSquare;
                
                //---------------------------Right part of if----------------------------------//     
                
                double fraction1,fraction2,sumFraction;
                
                fraction1 = Math.pow((items.get(5)[idmax]-Math.sqrt(items.get(0)[dmin]*items.get(1)[dmin])),2.0)/items.get(2)[idmax];
                 
                fraction2 = Math.pow((items.get(5)[idmin]+Math.sqrt(items.get(0)[dmin]*items.get(1)[dmin])),2.0)/items.get(2)[idmin];
                 
                sumFraction = fraction1 + fraction2;
                
                //---------------------------Permutation step 4----------------------------------//     
                if(sumSquare>sumFraction){
                
                    items.get(3)[dmin]=(double)idmin;
                    
                }
                else{
                    
                    for(int k=0; k<items.get(1).length; k++){System.out.println("Item " + (k+1) + " Sto Channel "+ items.get(3)[k]);}
                    System.out.println("/----------------------------------------------------------/");
                    return;
                
                }
               
                t++;
                
                for(int k=0; k<items.get(1).length; k++){System.out.println("Item " + (k+1) + " Sto Channel "+ items.get(3)[k]);}
                System.out.println("/----------------------------------------------------------/");
                br.close();
            
        }  

    }
    
}
    